#include"StdAfx.h"
/*
Constructor
*/
CLogfile::CLogfile()
{
	m_verbosityLevel = EVERBOSITY_LEVEL::ALL;
}

/*
Destructor
Write EOF and close file
*/
CLogfile::~CLogfile()
{

	Textout("<br><br>End of Logfile</font></body></html>");
	m_sWholeText.clear();
}

/*
CreateLogfile()
*/
int CLogfile::CreateLogfile(const char* sFilename)
{
	m_sFilename = sFilename;
	m_sWholeText = "";
	
	Textout("<html><head><title>PaleGem Logfile</title></head>");
	Textout("<body><font face='courier new'>");
	WriteTopic("PaleGem Logfile", 3);

	//current build config
#ifdef _DEBUG
	Textout("BUILD: Debug<br>");
#else
	Textout("BUILD: Release<br>");
#endif

	char* verbosity;

	switch(m_verbosityLevel)
	{
	case RELEASE:
		verbosity = "RELEASE"; break;
	//case DEBUG:
		//verbosity = "DEBUG"; break;
	case ALL:
		verbosity = "ALL"; break;
	}

	fTextout("Verbosity: %s <br><br>", verbosity); 

	//fclose (m_pLogfile);
	//m_pLogfile = fopen (sFilename, "w"); //opens the file in append mode
	//if (!m_pLogfile)
	//{
	//	//IMPLEMENT
	//	//MessageBox
	//	MessageBox(NULL, L"Failed to reopen Logfile!", L"Logfile could not be opended!", MB_OK);
	//	return false;
	//}

	//g_PG_pFrameworkEventManager->RegisterCriticalErrorListener(this);

	return true;
}

void CLogfile::WriteTopic(const char* sTopic, int size)
{
	Textout("<table cellspacing='0' cellpadding='0' width='100%%' ");
	Textout("bgcolor='#DFDFE5'><tr><td><font face='arial' ");
	fTextout("size='+%i'>\n", size);
	Textout(sTopic);
	Textout("</font></td></tr></table><br>");
	
	Textout("<!--ENDOF");
	Textout(sTopic);
	Textout("-->");

	fflush (m_pLogfile);
}

void CLogfile::Textout(const char* sText)
{
	m_sWholeText.append(sText);
	
	m_pLogfile = fopen (m_sFilename, "w");  // creates a file in write mode
	if (!m_pLogfile)
	{
		MessageBox(NULL, (LPCWSTR)"Failed to create Logfile!", LPCWSTR("PaleGem Error"), MB_OK);
		return;
	}
	fprintf (m_pLogfile, m_sWholeText.c_str());

	
	fflush (m_pLogfile);
	fclose(m_pLogfile);

}

void CLogfile::Textout(int Color, const char* sText)
{
	Textout(Color, false, sText);
}

void CLogfile::Textout(int Color, bool bList, const char* sText)
{
	if (bList == true)
		Textout("<li>");
	
	switch (Color)
	{
	case BLACK:
		{
			Textout("<font color=black>");
		}break;
	case RED:
		{
			Textout("<font color=red>");
		}break;
	case GREEN:
		{
			Textout("<font color=green>");
		}break;
	case BLUE:
		{
			Textout("<font color=blue>");
		}break;
	case PURPLE:
		{
			Textout("<font color=purple>");	
		}break;
	case YELLOW:
		{
			Textout("<font color=FF9400>");
		}
	};

	Textout(sText);
	Textout("</font>");

	if (bList == false)
		Textout("<br>");
	else
		Textout("</li>");
}

void CLogfile::fTextout(const char* sText,...)
{
	//TCHAR buffer[MAX_BUFFER];
	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, sText);
	vsprintf (buffer, sText, pArgList);
	va_end (pArgList);

	Textout(buffer);
}

void CLogfile::fTextout(int Color, const char* sText,...)
{
	//TCHAR buffer[MAX_BUFFER];
	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, sText);
	vsprintf (buffer, sText, pArgList);
	va_end (pArgList);

	Textout(Color, buffer);
}

void CLogfile::fTextout(int Color, bool bList, const char* sText,...)
{
	//TCHAR buffer[MAX_BUFFER];
	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, sText);
	vsprintf (buffer, sText, pArgList);
	va_end (pArgList);

	Textout(Color, bList, buffer);

}

void CLogfile::FunctionResult(const char* sName, bool bResult)
{
	if (true == bResult)
	{
		Textout("<table width='100%%' cellSpacing='1' cellPadding='5'");
		Textout("</table>");
	}
}

void CLogfile::fTextoutTopic(const char* sTopic, const char* sText, ...)
{
	// prepare the topic string to be searched
	size_t pos;
	std::string sTopicBuffer;

	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, sText);
	vsprintf (buffer, sText, pArgList);
	va_end (pArgList);




	std::string sTextBuffer(buffer);
	sTextBuffer.append("<br>");

	sTopicBuffer.append("<!--ENDOF");
	sTopicBuffer.append(sTopic);
	sTopicBuffer.append("-->");

	// search in the string
	pos = m_sWholeText.find(sTopicBuffer);

	// not found, so create new topic
	if (pos == std::string::npos)
	{
		WriteTopic(sTopic, 2);
		//Textout(sTextBuffer.c_str());
		pos = m_sWholeText.find(sTopicBuffer);
	}
	// found set the coursor to that position
	m_sWholeText.insert(pos, sTextBuffer.c_str()); 

}

void CLogfile::fTextoutTopic (int Color, const char* sTopic, const char* sText, ...)
{
	// prepare the topic string to be searched
	std::string sTextBuffer;

	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, sText);
	vsprintf (buffer, sText, pArgList);
	va_end (pArgList);

	switch (Color)
	{
	case BLACK:
		{
			sTextBuffer.append("<font color=black>");
		}break;
	case RED:
		{
			sTextBuffer.append("<font color=red>");
		}break;
	case GREEN:
		{
			sTextBuffer.append("<font color=green>");
		}break;
	case BLUE:
		{
			sTextBuffer.append("<font color=blue>");
		}break;
	case PURPLE:
		{
			sTextBuffer.append("<font color=purple>");	
		}break;
	case YELLOW:
		{
			sTextBuffer.append("<font color=goldenrod>");
		}
	};

	
	sTextBuffer.append(buffer);
	sTextBuffer.append("</font>");
	//sTextBuffer.append("<br>");

	fTextoutTopic(sTopic, sTextBuffer.c_str());

	/*

	sTopicBuffer.append("<!--ENDOF");
	sTopicBuffer.append(sTopic);
	sTopicBuffer.append("-->");

	
	
	// search in the string
	pos = m_sWholeText.find(sTopicBuffer);

	// not found, so create new topic
	if (pos == std::string::npos)
	{
		WriteTopic(sTopic, 2);
		//Textout(Color, sTextBuffer.c_str());
		pos = m_sWholeText.find(sTopicBuffer);

		if (pos != std::string::npos)
		{
			m_sWholeText.insert(pos, sTextBuffer.c_str());
		}
	}
	// found set the coursor to that position
	else
	{
		m_sWholeText.insert(pos, sTextBuffer.c_str()); 
	}
	*/
	
}

void CLogfile::LogWarning(const char* szTopic, const char* szWarning, ...)
{
	char buffer[MAX_BUFFER];
	va_list pArgList;

	va_start (pArgList, szWarning);
	vsprintf (buffer, szWarning, pArgList);
	va_end (pArgList);

	fTextoutTopic(YELLOW, szTopic, "WARNING: %s", buffer);
}
